﻿using DeviceAlarmService.MessagePush.Communication;
using DeviceAlarmService.MessagePush.Play;
using DeviceAlarmService.Models;
using DeviceAlarmService.Models.Message;
using DeviceAlarmService.SystemService;

namespace DeviceAlarmService.MessagePush;

/// <summary>
/// 消息推送
/// </summary>
public class MessagePush
{
    /// <summary>
    /// 操作委托
    /// </summary>
    /// <param name="status"></param>
    public delegate void LogDelegate(string log);
    /// <summary>
    /// 操作事件
    /// </summary>
    /// <param name="status"></param>
    public event LogDelegate LogEvent;

    public void OperationLog(string log)
    {
        if (LogEvent is not null)
        {
            LogEvent($"{log}");
        }
    }

    public void Receive(List<Contacts> contacts, string name, string information, Level level = Level.User)
    {
        //获取模板
        string template;
        template = ConfigService.Configuration.template;
        template = template.Replace("name", name);
        template = template.Replace("date", DateTime.Now.ToString("F"));
        template = template.Replace("information", information);
        template = template.Replace("level", level.ToString());

        OperationLog(template);
        Send(contacts, template, level);
    }

    /// <summary>
    /// 信息发送
    /// </summary>
    /// <param name="contacts"></param>
    /// <param name="template"></param>
    public void Send(List<Contacts> contacts, string template, Level level = Level.User)
    {
        //语音播报队列
        PlayService voiceAnnouncements = new();
        voiceAnnouncements.Add(template);

        List<string> contactsList = new();
        //联系人合并
        if (contacts != null)
        {
            foreach (var itme in contacts)
            {
                if (itme.enableSms)
                {
                    contactsList.Add(itme.phoneNumber);
                }
            }
        }
        //全局联系人
        if (ConfigService.Configuration.contacts is not null && ConfigService.Configuration.contacts.Count >= 1)
        {
            foreach (var itme in contacts)
            {
                if ((int)itme.level >= (int)level && itme.enableSms)
                {
                    contactsList.Add(itme.phoneNumber);
                }
            }
        }
        //短信发送
        PushService.Add(MessageTypeEnum.SMS, contactsList, template);

        List<string> contactsLists = new();
        //联系人合并
        if (contacts != null)
        {
            foreach (var itme in contacts)
            {
                if (itme.enableDialing)
                {
                    contactsLists.Add(itme.phoneNumber);
                }
            }
        }
        //全局联系人
        if (ConfigService.Configuration.contacts is not null && ConfigService.Configuration.contacts.Count >= 1)
        {
            foreach (var itme in contacts)
            {
                //全局级别分类 未有效处理
                if ((int)itme.level >= (int)level && itme.enableDialing)
                {
                    contactsLists.Add(itme.phoneNumber);
                }
            }
        }
        PushService.Add(MessageTypeEnum.CALL, contactsLists, template);
    }
}